package com.dqyt.khjc.service;

import com.dqyt.common.check.CheckTool;
import com.dqyt.common.constant.AuthorityID;
import com.dqyt.common.utils.RegularUtil;
import com.dqyt.common.utils.UserInfoUtil;
import com.dqyt.khjc.bean.ManufacturerBean;
import com.dqyt.khjc.bean.RespPageBean;
import com.dqyt.khjc.bean.ResultMsg;
import com.dqyt.khjc.bean.User;
import com.dqyt.khjc.mapper.ManufacturerMapper;
import com.dqyt.khjc.mapper.ModelMapper;
import com.dqyt.khjc.utils.TimeUtil;
import org.springframework.beans.factory.annotation.Autowire;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
public class ManufacturerService {

    @Autowired
    private ManufacturerMapper manufacturerMapper;

    @Autowired
    private ModelMapper modelMapper;


    /**
     * 查询制造厂商列表
     * @param request
     * @return
     */
    public ResultMsg getManufacturers(HttpServletRequest request){
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            // 检查用户是否登录Token是否正确
            ret = UserInfoUtil.isAuthori_Web(request);
            // 如果有错误信息
            if (ret != null) {
                return ret;
            }
            ret = new ResultMsg(); // 声明一个返回值
            // 权限检查
            // 首先取得用户信息
            User user = UserInfoUtil.getUser_Web(request);
            //查询
            List<ManufacturerBean> data  = manufacturerMapper.getManufacturers();
            ret.setCode(ResultMsg.SUCESS);
            ret.setMsg("查询成功");
            ret.setData(data);
        }catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }


    /**
     * 分页查询
     * @param request
     * @param name
     * @param enname
     * @return
     */
    public ResultMsg getZzcsListPage(HttpServletRequest request ,Integer page, Integer size, String name, String enname){
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            // 检查用户是否登录Token是否正确
            ret = UserInfoUtil.isAuthori_Web(request);
            // 如果有错误信息
            if (ret != null) {
                return ret;
            }
            ret = new ResultMsg(); // 声明一个返回值
            User user = UserInfoUtil.getUser_Web(request);
            if (page != null && size != null) {
                page = (page - 1) * size;
            }
            //查询
            List<ManufacturerBean> data  = manufacturerMapper.getZzcsListPage(page,size,name,enname);
            long  total = manufacturerMapper.getZzcsTotalNumber(name,enname);
            RespPageBean bean = new RespPageBean();
            bean.setData(data);
            bean.setTotal(total);
            ret.setCode(ResultMsg.SUCESS);
            ret.setData(bean);
        }catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }


   public ResultMsg addZzcs(HttpServletRequest request,String name,String enname,String abridge){
       ResultMsg ret = new ResultMsg(); // 声明一个返回值
       try {

           // 检查用户是否登录Token是否正确
           ret = UserInfoUtil.isAuthori_Web(request);
           // 如果有错误信息
           if (ret != null) {
               return ret;
           }
           ret = new ResultMsg(); // 声明一个返回值
           User user = UserInfoUtil.getUser_Web(request);

           // 用户权限检查 是否可以新增客户信息
//           if (!CheckTool.checkAuthority(user, AuthorityID.JCSJGL_SCCS)) {
//               ret.setCode(ResultMsg.ERROR);
//               ret.setMsg("你没有对应的操作权限");
//               return ret;
//           }


           //添加的时候根据名字查询是否有数据
           long retotl = manufacturerMapper.getmanufacturerByName(name);
           if(retotl>0){
               ret.setCode(ResultMsg.ERROR);
               ret.setMsg("该制造厂商已经存在");
               return ret;
           }

           long retotl2 = manufacturerMapper. getmanufacturerByenName(enname);
           if(retotl2>0){
               ret.setCode(ResultMsg.ERROR);
               ret.setMsg("该商标已经存在");
               return ret;
           }


           long retotl3 = manufacturerMapper.getmanufacturerByenAbridge(abridge);
           if(retotl3>0){
               ret.setCode(ResultMsg.ERROR);
               ret.setMsg("缩写名称经存在");
               return ret;
           }

           ManufacturerBean manufacturerBean = new ManufacturerBean();
           manufacturerBean.setName(name);
           manufacturerBean.setAbridge(abridge);


           manufacturerBean.setEnname(enname);
           manufacturerBean.setAddress("");
           long tt = TimeUtil.currentTimeMillis();
           manufacturerBean.setAddTime(tt);
           manufacturerBean.setAddUser(user.getUserId()+"");
           manufacturerBean.setUpdateTime(tt);
           manufacturerBean.setUpdateUser(user.getUserId()+"");
           //新增制造厂商
           int res = manufacturerMapper.addManufacturers(manufacturerBean);
           if(res>0){
               ret.setCode(ResultMsg.SUCESS);
               ret.setMsg("新增成功");
           }else{
               ret.setCode(ResultMsg.ERROR);
               ret.setMsg("数据异常");
           }
       }catch (Exception e) {
           ret.setCode(ResultMsg.ERROR);
           ret.setMsg("未知的错误，请稍候再试！");
           e.printStackTrace();
       }
       return ret;
   }


    /**
     * 删除制造厂商
     * @param request
     * @param id
     * @return
     */
   public ResultMsg delZzcs(HttpServletRequest request,long id){
       ResultMsg ret = new ResultMsg(); // 声明一个返回值
       try {
           // 检查用户是否登录Token是否正确
           ret = UserInfoUtil.isAuthori_Web(request);
           // 如果有错误信息
           if (ret != null) {
               return ret;
           }
           ret = new ResultMsg(); // 声明一个返回值
           User user = UserInfoUtil.getUser_Web(request);

           // 用户权限检查 是否可以新增客户信息
//           if (!CheckTool.checkAuthority(user, AuthorityID.JCSJGL_SCCS)) {
//               ret.setCode(ResultMsg.ERROR);
//               ret.setMsg("你没有对应的操作权限");
//               return ret;
//           }


           long res =  manufacturerMapper.delZzcs(id);
           //同时删除制造厂商下面的所有规格型号
           long res2 =modelMapper.delByparentId(id);
           if(res>0){
               ret.setCode(ResultMsg.SUCESS);
               ret.setMsg("删除成功");
           }else{
               ret.setCode(ResultMsg.ERROR);
               ret.setMsg("未知的错误，请稍候再试！");
           }
       }catch (Exception e) {
           ret.setCode(ResultMsg.ERROR);
           ret.setMsg("未知的错误，请稍候再试！");
           e.printStackTrace();
       }
       return ret;
    }




    /**
     * 删除制造厂商
     * @param request
     * @param id
     * @return
     */
    public ResultMsg updateZzcs(HttpServletRequest request,long id,String name,String enname,String abridge){
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            // 检查用户是否登录Token是否正确
            ret = UserInfoUtil.isAuthori_Web(request);
            // 如果有错误信息
            if (ret != null) {
                return ret;
            }
            ret = new ResultMsg(); // 声明一个返回值
            User user = UserInfoUtil.getUser_Web(request);
            // 用户权限检查 是否可以新增客户信息
//            if (!CheckTool.checkAuthority(user, AuthorityID.JCSJGL_SCCS)) {
//                ret.setCode(ResultMsg.ERROR);
//                ret.setMsg("你没有对应的操作权限");
//                return ret;
//            }


             ManufacturerBean manu1 = manufacturerMapper.getmanufacturerByName2(name);
             if(null != manu1){
                if(manu1.getId() != id && manu1.getName().equals(name)){
                    ret.setCode(ResultMsg.ERROR);
                    ret.setMsg("该制造厂商已经存在");
                    return ret;
                }

             }
            ManufacturerBean manu2 = manufacturerMapper. getmanufacturerByenName2(enname);
             if(null !=manu2){
                if(manu2.getId() != id && manu2.getEnname().equals(enname)){
                    ret.setCode(ResultMsg.ERROR);
                    ret.setMsg("该商标已经存在");
                    return ret;
                }
             }

            ManufacturerBean manu3 = manufacturerMapper. getmanufacturerByenAbridge2(abridge);
            if(null !=manu3){
                if(manu3.getId() != id && manu3.getAbridge().equals(abridge)){
                    ret.setCode(ResultMsg.ERROR);
                    ret.setMsg("该缩写已经存在");
                    return ret;
                }
            }

            long res =  manufacturerMapper.updateZzcs(id,name,enname,abridge);
            if(res>0){
                ret.setCode(ResultMsg.SUCESS);
                ret.setMsg("修改成功");
            }else{
                ret.setCode(ResultMsg.ERROR);
                ret.setMsg("未知的错误，请稍候再试！");
            }


        }catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }



}
